package com.edu.controller;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.edu.exception.FileException;
import com.edu.service.FileService;
import com.edu.util.IOHelper;
import com.edu.util.ZFilenameUtils;
import com.edu.vo.FileMeta;
import com.edu.vo.FileVO;
import com.edu.vo.PageUtils;
import com.edu.vo.ResultVO;
import com.edu.vo.ServerConstant;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PageUtils getFileList(@RequestParam(value = "dir", required = false) String dir) {
        dir = dir == null ? "" : dir;
        // PageUtils page;
        // logger.debug(dir);
        List<FileVO> list = null;
        try {
            list = fileService.enterDirectory(dir);
        } catch (FileException e) {
            e.printStackTrace();
            return new PageUtils(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
        }
        // logger.debug("xxx:{}",dir);
        return new PageUtils(list, list.size());
    }

    @RequestMapping(value = "/content", produces = "application/json; charset=UTF-8", method = RequestMethod.POST)
    public ResultVO getFileContent(@RequestParam(value = "name", required = false) String name) {

        String document = null;
        try {
            document = fileService.showDocument(name);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultVO.getErrorResultVO(e.getMessage());
        }
        logger.debug(document);
        return ResultVO.getSuccessWithMessage(document);

    }

    @RequestMapping(value = "/content/editor", method = RequestMethod.POST)
    public ResultVO editFileContent(@RequestParam(value = "name", required = false) String name,
                                    @RequestParam(value = "text", required = false) String text) {
        try {
            fileService.editDocument(name, text);
        } catch (FileException e) {
            e.printStackTrace();
            return new ResultVO(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
        }
        return ResultVO.getSuccessResultVO();
    }

    @RequestMapping(value = "/rename", method = RequestMethod.GET)
    public ResultVO renameFile(@RequestParam(value = "name") String name,
                               @RequestParam(value = "rename") String rename) {
        // logger.debug("rename");

        // 数据校验
        if (!ZFilenameUtils.isValidDirName(rename)) {
            return ResultVO.getErrorResultVO(FileException.NAME_ILLEGAL);
        }

        try {
            fileService.renameFile(name, rename);
        } catch (IOException | FileException e) {
            e.printStackTrace();
            return new ResultVO(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
        }
        return ResultVO.getSuccessResultVO();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResultVO deleteFile(@RequestParam(value = "name") String name) {
        // logger.debug("rename");
        try {
            fileService.deleteFile(name);
        } catch (IOException | FileException e) {
            e.printStackTrace();
            return new ResultVO(ServerConstant.SERVER_INTERNAL_ERROR, e.getMessage());
        }
        return ResultVO.getSuccessResultVO();
    }

    @RequestMapping(value = "/copy", method = RequestMethod.GET)
    public ResultVO deleteFile(@RequestParam(value = "name") String name,
                               @RequestParam(value = "targetDir") String targetDir) {

        // 数据校验
        if (!ZFilenameUtils.isValidDirName(targetDir)) {
            return ResultVO.getErrorResultVO(FileException.NAME_ILLEGAL);
        }
        try {
            fileService.copyToDir(name, targetDir);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultVO.getErrorResultVO(e.getMessage());
        }
        return ResultVO.getSuccessResultVO();
    }


}
